1_
Create simple web service as .asmx
C# <%@ WebService Language="c#" Class="MyServiceNs.WebServiceSimple" %> using System.Web.Services; namespace MyServiceNs { [WebService(Namespace="http://localhost/myservice")] public class WebServiceSimple : WebService { [WebMethod(Description="This method sums 2 integers")] public int Add(int a, int b) { return a+b; } } }
2_
How to deploy web service (3 options)
- create WebServiceSimpleEmbedded.asmx file and copy paste the code sample above
- create c:\intepub\wwwroot\WebServiceSimple folder and copy the .asmx file under the folder
- Open IIS Manager
- expand Default web site node
- right click on WebServiceSimple and select Convert to Application. This will associate an Application pool with your web service.
Option 2: split the .asmx file above into header in .asmx and code App_Code\*.asmx.cs
Option 3: compile web service code into a .dll a put it under \bin folder
- create c:\intepub\wwwroot\WebServiceSimple folder and copy the .asmx file under the folder
- Open IIS Manager
- expand Default web site node
- right click on WebServiceSimple and select Convert to Application. This will associate an Application pool with your web service.
Deployment options
Option 3: compile web service code into a .dll a put it under \bin folder
3_
How to get web service definition (WSDL)
To see web service definition in SOAP, type http://localhost/WebServiceSimple/WebServiceSimpleEmbedded.asmx?WSDL
4_
What needs to be done in order to access a web method directly from URL
XML <configuration> <system.web> <webServices> <protocols> <add name="HttpGet" /> </protocols> </webServices> </system.web> </configuration>
5_
How to access method from HTML form
HTML <FORM action="http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx/Add" method="post"> A <input type="text" name="a"> + B <input type="text" name="b"> <input type="submit" value="Calculate"> </FORM>
6_
Create proxy
In order to call web service from a .net client you need to create a proxy. You can do it from command line
CMD "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\WSDL.exe" http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx?WSDL /out:WebServiceSimpleProxy.csanother option is to create web service proxy from Visual Studio by right clicking on the References folder and selecting Add service reference
7_
Implement .net client in C# that calls a method
You need to include WebServiceSimpleProxy.cs that defines "WebServiceSimple" client class in your project and compile the code below
C# using System; namespace Client { class Program { static void Main(string[] args) { WebServiceSimple client = new WebServiceSimple(); // class defined in proxy int result = client.Add(1, 2); client.Close(); } } }
8_
How to use Session
C# public class SampleService : System.Web.Services.WebService { [WebMethod(Description = "This method sums 2 integers", EnableSession=true)] public int Add(int a, int b) { if (Session["AccessCounter"] == null) { Session["AccessCounter"] = 1; } // using Session requires "EnableSession=true" attribute else { Session["AccessCounter"] = ((Int32)Session["AccessCounter"]) + 1; } return a + b; } [WebMethod(EnableSession=true)] public int AccessCount() { if (Session["AccessCounter"] == null) { return 0; } else { return (Int32)Session["AccessCounter"]; } } }